package com.ykyl.controller.security;


import com.ykyl.base.Result;
import com.ykyl.pojo.dto.ResourceDto;
import com.ykyl.pojo.vo.MenuVo;
import com.ykyl.pojo.vo.ResourceVo;
import com.ykyl.pojo.vo.TreeVo;
import com.ykyl.service.ResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资源前端控制器
 */
@Slf4j
@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Autowired
    ResourceService resourceService;

    /**
     * @param resourceDto 资源Vo对象
     * @return ResourceVo
     *  保存资源
     */
    @PutMapping
    public Result<ResourceVo> createResource(@RequestBody ResourceDto resourceDto) {
        return Result.success(resourceService.createResource(resourceDto));
    }

    /**
     * @param resourceDto 资源DTO对象
     * @return Boolean 是否修改成功
     *  修改资源
     */
    @PatchMapping
    public Result<Boolean> updateResource(@RequestBody ResourceDto resourceDto) {
        return Result.success(resourceService.updateResource(resourceDto));
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("/{menuId}")
    public Result remove(@PathVariable("menuId") String menuId) {
        return Result.success(resourceService.deleteMenuById(menuId));
    }

    /***
     *  多条件查询资源列表
     * @param resourceDto 资源Vo对象
     * @return List<ResourceVo>
     */
    @PostMapping("/list")
    public Result<List<ResourceVo>> resourceList(@RequestBody ResourceDto resourceDto) {
        List<ResourceVo> resourceVoList = resourceService.findResourceList(resourceDto);
        return Result.success(resourceVoList);
    }

    /**
     * @param resourceDto 资源对象
     * @return
     *  资源树形
     */
    @PostMapping("/tree")
    public Result<TreeVo> resourceTreeVo(@RequestBody ResourceDto resourceDto) {
        TreeVo treeVo = resourceService.resourceTreeVo(resourceDto);
        return Result.success(treeVo);
    }

    /**
     * @return
     *  左侧菜单
     */
    @GetMapping("/menus")
    public Result<List<MenuVo>> menus() {
        List<MenuVo> menus = resourceService.menus();
        return Result.success(menus);
    }

    /**
     * @return
     *  按钮列表
     */
    @GetMapping("/myButten")
    public Result<List<MenuVo>> buttons() {
        List<MenuVo> menus = resourceService.buttons();
        return Result.success(menus);
    }

}
